package cn.xiaozhu.Controller;

import cn.xiaozhu.service.facade.IUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.UnsupportedEncodingException;

/**
 * 用户登陆请求
 * @author shkstart
 * @date 2019/8/14 - 15:49
 */
@Controller
public class LoginController {
    /**
     * 访问userService
     */
    @Autowired
    private IUserService userService;
    private final static Logger logger = LoggerFactory.getLogger(LoginController.class);

    /**
     * 用户登陆
     * @param userName
     * @param password
     * @return
     */
    @RequestMapping(path = "/login", method = RequestMethod.POST)
    public String hello(@RequestParam("userName") String userName, String password,  ModelMap model) throws
            UnsupportedEncodingException {
        if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(password)) {
            logger.debug("false");
            return "loginfail";
        } else {
            boolean flag = userService.login(userName, password);
            if (flag == true) {
                model.addAttribute("tt",userName);
                logger.debug("login");
                return "login";
            } else {
                return "loginfail";
            }



        }
    }
}






